package level.editor.model.main;

import java.io.File;
import java.util.Stack;

import level.model.State;
import level.model.utility.StateHandler;

public class StateMemento
{
	public StateMemento(State state)
	{
		undo = new Stack<State>();
		redo = new Stack<State>();
		undo.push(state);
		file = state.getFile();
	}

	public void add(State state)
	{
		undo.push(state);
		undo.peek().setFile(file);
		redo.clear();
	}

	public boolean undo(MainModel mod)
	{
		if (undo.size() > 1)
		{
			redo.push(undo.pop());
			undo.peek().setFile(file);
			mod.setState(StateHandler.copy(undo.peek()));
			return true;
		}
		else
			return false;
	}

	public boolean redo(MainModel mod)
	{
		if (!redo.isEmpty())
		{
			undo.push(redo.pop());
			undo.peek().setFile(file);
			mod.setState(StateHandler.copy(undo.peek()));
			return true;
		}
		else
			return false;
	}

	public void updateFile(File file)
	{
		this.file = file;
	}

	private Stack<State> undo;
	private Stack<State> redo;
	private File file;
}
